// token 信息
// 路由组件
import Layout from "@/layouts";
import store from "@/store";
//admin
import Admin from "@/views/my-views/Admin/Admin";
import AssetsType from "@/views/my-views/Capital/assets-type/AssetsType";
import Assets from "@/views/my-views/Capital/assets/Assets";
import AssetsCreate from "@/views/my-views/Capital/assets/TableEdit";
import Funds from "@/views/my-views/Capital/funds/Funds";
import FundsCreate from "@/views/my-views/Capital/funds/TableEdit";
import ProjectConstruction from "@/views/my-views/Capital/project-construction/ProjectConstruction";
import ProjectConstructionCreate from "@/views/my-views/Capital/project-construction/TableEdit";
//capital
import ResourcesType from "@/views/my-views/Capital/resources-type/ResourcesType";
import Resources from "@/views/my-views/Capital/resources/Resources";
import ResourcesCreate from "@/views/my-views/Capital/resources/TableEdit";
//feedback
import CreateFeedback from "@/views/my-views/Feedback/CreateFeedback";
import Processed from "@/views/my-views/Feedback/Processed";
import ToBeProcessed from "@/views/my-views/Feedback/ToBeProcessed";
import Upward from "@/views/my-views/Feedback/Upward";
//info
import Area from "@/views/my-views/Info/area-info/Area";
import Group from "@/views/my-views/Info/group-info/Group";
import Migrate from "@/views/my-views/Info/migrate/migrate";
import MigrateCreate from "@/views/my-views/Info/migrate/TableEdit";
import PartyMember from "@/views/my-views/Info/party-member/PartyMember";
import PartyMemberCreate from "@/views/my-views/Info/party-member/TableEdit";
import SubsidyInfo from "@/views/my-views/Info/subsidy-info/SubsidyInfo";
import SubsidyType from "@/views/my-views/Info/subsidy-type/SubsidyType";
import Subsidy from "@/views/my-views/Info/subsidy/Subsidy";
import SubsidyCreate from "@/views/my-views/Info/subsidy/TableEdit";
import Town from "@/views/my-views/Info/town-info/Town";
import VillageCadreCreate from "@/views/my-views/Info/village-cadre/TableEdit";
import VillageCadre from "@/views/my-views/Info/village-cadre/VillageCadre";
import VillageCreate from "@/views/my-views/Info/village/TableEdit";
import Village from "@/views/my-views/Info/village/village";
import VillagerCreate from "@/views/my-views/Info/villager/TableEdit";
import Villager from "@/views/my-views/Info/villager/villager";
//admin
import Logger from "@/views/my-views/Log/Log";
import { default as AssetsWrap, default as OrgActionWrap, default as ResourcesWrap, default as SubsidyWrap, default as VillageActionWrap } from "@/views/my-views/mainWrap";
// login
import MyLogin from "@/views/my-views/my-login";
import LearningResources from "@/views/my-views/Release/learning-resources/LearningResources";
import OrgActionType from "@/views/my-views/Release/org-action-type/OrgActionType";
import OrgAction from "@/views/my-views/Release/org-action/OrgAction";
import OrgActionCreate from "@/views/my-views/Release/org-action/TableEdit";
import VillageActionType from "@/views/my-views/Release/village-action-type/VillageActionType";
import VillageActionCreate from "@/views/my-views/Release/village-action/TableEdit";
import VillageAction from "@/views/my-views/Release/village-action/VillageAction";
import VillageBulletinBoard from "@/views/my-views/Release/village-bulletin-board/VillageBulletinBoard";
//release
import VillageMeetingCreate from "@/views/my-views/Release/village-meeting/TableEdit";
import VillageMeeting from "@/views/my-views/Release/village-meeting/VillageMeeting";
// VUE
import Vue from "vue";
import VueRouter from "vue-router";




Vue.use(VueRouter);

/*当settings.js里authentication配置的是intelligence时，views引入交给前端配置*/
export const asyncRoutes = [
  {
    path: "/",
    component: Layout,
    redirect: "index",
    children: [
      {
        path: "index",
        name: "Index",
        component: () => import("@/views/index/index"),
        meta: {
          title: "首页",
          icon: "home",
          affix: true,
          badge: "New",
          "permissions": ["root", "admin", "leader", "area", "town", "village"]
        },
      },
    ],
  },
  
  //我的 业务
  {
    path: "/info",
    component: Layout,
    redirect: "noRedirect",
    name: "Info",
    meta: { title: "信息管理", icon: "database" },
    children: [
      {
        path: "area",
        name: "Area",
        component: Area,
        meta: { title: "区信息", "permissions": ["root", "admin", "leader"] },
      },
      {
        path: "town",
        name: "Town",
        component: Town,
        meta: { title: "镇(街道)信息", "permissions": ["root", "admin", "leader", "area", ] },
      },
      {
        path: "village",
        name: "Village",
        component: Village,
        meta: { title: "行政村信息", "permissions": ["root", "admin", "leader", "area", "town", "village"] },
      },
      {
        path: "village-create",
        name: "VillageCreate",
        component: VillageCreate,
        meta: { 
          title: "行政村信息新增", 
          "permissions": ["root", "admin", "leader", "area", "town",], 
          activeMenu: "/info/village" 
        },
        hidden: true,
      },
      {
        path: "village-edit/:id",
        name: "VillageEdit",
        component: VillageCreate,
        props: true,
        meta: { 
          title: "行政村信息编辑", 
          "permissions": ["root", "admin", "leader", "area", "town", "village"],
          activeMenu: "/info/village" 
      },
        hidden: true,
      },
      {
        path: "group",
        name: "Group",
        component: Group,
        meta: { title: "行政村组信息",  },
      },
      {
        path: "villager/:pageNo",
        name: "Villager",
        component: Villager,
        props: true,
        meta: { activeMenu: "/info/villager" },
        hidden: true,
      },
      {
        path: "villager",
        name: "Villager",
        component: Villager,
        meta: { title: "用户信息", activeMenu: "/info/villager"  },
      },
      {
        path: "villager-create",
        name: "VillagerCreate",
        component: VillagerCreate,
        meta: { title: "用户信息新增", activeMenu: "/info/villager" },
        hidden: true,
      },
      {
        path: "villager-edit/:id/:pageNo",
        name: "VillagerEdit",
        component: VillagerCreate,
        props: true,
        meta: { title: "用户信息编辑", activeMenu: "/info/villager"  },
        hidden: true,
      },
      {
        path: "migrate",
        name: "Migrate",
        component: Migrate,
        meta: { title: "户口迁移" },
      },
      {
        path: "migrate-create",
        name: "MigrateCreate",
        component: MigrateCreate,
        meta: { title: "迁移信息新增", activeMenu: "/info/migrate"  },
        hidden: true,
      },
      {
        path: "migrate-edit/:id",
        name: "MigrateEdit",
        component: MigrateCreate,
        props: true,
        meta: { title: "迁移信息编辑", activeMenu: "/info/migrate"  },
        hidden: true,
      },
      {
        path: "about-subsidy",
        name: "AboustSubsidy",
        component: SubsidyWrap,
        meta: { title: "行政村补助信息" },
        children: [
          {
            path: "subsidy-type",
            name: "SubsidyType",
            component: SubsidyType,
            meta: { title: "补助项类型" },
          },
          {
            path: "subsidy",
            name: "Subsidy",
            component: Subsidy,
            meta: { title: "补助项" },
          },
          {
            path: "subsidy-create",
            name: "SubsidyCreate",
            component: SubsidyCreate,
            meta: { title: "补助项信息新增", activeMenu: "/info/about-subsidy/subsidy"  },
            hidden: true,
          },
          {
            path: "subsidy-edit/:id",
            name: "SubsidyEdit",
            component: SubsidyCreate,
            props: true,
            meta: { title: "补助项信息编辑", activeMenu: "/info/about-subsidy/subsidy"  },
            hidden: true,
          },
          {
            path: "subsidy-info",
            name: "SubsidyInfo",
            component: SubsidyInfo,
            meta: { title: "补助信息" },
          },
        ],
      },
      
      
      {
        path: "village-cadre",
        name: "VillageCadre",
        component: VillageCadre,
        meta: { title: "村干部信息" },
      },
      {
        path: "village-cadre-create",
        name: "VillageCadreCreate",
        component: VillageCadreCreate,
        meta: { title: "村干部新增", activeMenu: "/info/village-cadre"  },
        hidden: true,
      },
      {
        path: "village-cadre-edit/:id",
        name: "VillageCadreEdit",
        component: VillageCadreCreate,
        props: true,
        meta: { title: "村干部编辑", activeMenu: "/info/village-cadre"  },
        hidden: true,
      },
      {
        path: "party-member",
        name: "PartyMember",
        component: PartyMember,
        meta: { title: "党员发展" },
      },
      {
        path: "party-member-create",
        name: "PartyMemberCreate",
        component: PartyMemberCreate,
        meta: { title: "党员新增", activeMenu: "/info/party-member"  },
        hidden: true,
      },
      {
        path: "party-member-edit/:id",
        name: "PartyMemberEdit",
        component: PartyMemberCreate,
        props: true,
        meta: { title: "党员编辑", activeMenu: "/info/party-member"  },
        hidden: true,
      },
    ],
  },
  {
    path: "/capital",
    component: Layout,
    redirect: "noRedirect",
    name: "Capital",
    meta: { title: "三资管理", icon: "donate" },
    children: [
      {
        path: "about-resources",
        component: ResourcesWrap,
        name: "AboutResources",
        meta: { title: "资源" },
        children: [
          {
            path: "resources-type",
            component: ResourcesType,
            name: "ResourcesType",
            meta: { title: "资源类型" },
          },
          {
            path: "resources",
            component: Resources,
            name: "Resources",
            meta: { title: "资源信息" },
          },
          {
            path: "resources-create",
            component: ResourcesCreate,
            name: "ResourcesCreate",
            meta: { title: "资源新增", activeMenu: "/capital/about-resources/resources"  },
            hidden: true,
          },
          {
            path: "resources-edit/:id",
            name: "ResourcesEdit",
            component: ResourcesCreate,
            props: true,
            meta: { title: "资源编辑", activeMenu: "/capital/about-resources/resources"  },
            hidden: true,
          },
        ],
      },
      {
        path: "about-assets",
        component: AssetsWrap,
        name: "AboutAssets",
        meta: { title: "资产" },
        children: [
          {
            path: "assets-type",
            component: AssetsType,
            name: "AssetsType",
            meta: { title: "资产类型" },
          },
          {
            path: "assets",
            component: Assets,
            name: "Assets",
            meta: { title: "资产信息" },
          },
          {
            path: "assets-create",
            component: AssetsCreate,
            name: "AssetsCreate",
            meta: { title: "资产新增", activeMenu: "/capital/about-assets/assets"  },
            hidden: true,
          },
          {
            path: "assets-edit/:id",
            component: AssetsCreate,
            name: "AssetsEdit",
            props: true,
            meta: { title: "资产编辑", activeMenu: "/capital/about-assets/assets"  },
            hidden: true,
          },
        ],
      },
      {
        path: "funds",
        component: Funds,
        name: "Funds",
        meta: { title: "资金" },
      },
      {
        path: "funds-create",
        component: FundsCreate,
        name: "FundsCreate",
        meta: { title: "资金新增", activeMenu: "/capital/funds"  },
        hidden: true,
      },
      {
        path: "funds-edit/:id",
        component: FundsCreate,
        name: "FundsEdit",
        props: true,
        meta: { title: "资金编辑", activeMenu: "/capital/funds"  },
        hidden: true,
      },
      {
        path: "project-construction",
        component: ProjectConstruction,
        name: "ProjectConstruction",
        meta: { title: "项目建设" },
      },
      {
        path: "project-construction-create",
        component: ProjectConstructionCreate,
        name: "ProjectConstructionCreate",
        meta: { title: "项目建设新增", activeMenu: "/capital/project-construction"  },
        hidden: true,
      },
      {
        path: "project-construction-edit/:id",
        component: ProjectConstructionCreate,
        name: "ProjectConstructionEdit",
        props: true,
        meta: { title: "项目建设编辑", activeMenu: "/capital/project-construction"  },
        hidden: true,
      },
    ],
  },
  {
    path: "/release",
    component: Layout,
    redirect: "noRedirect",
    name: "Release",
    meta: { title: "发布管理", icon: "bullhorn" },
    children: [
      {
        path: "learning-resources",
        component: LearningResources,
        name: "LearningResources",
        meta: { title: "学习园地" },
      },
      {
        path: "village-bulletin-board",
        component: VillageBulletinBoard,
        name: "VillageBulletinBoard",
        meta: { title: "村告栏" },
      },
      {
        path: "village-meeting",
        name: "VillageMeeting",
        component: VillageMeeting,
        meta: { title: "村会议" }
      },
      {
        path: "village-meeting-create",
        name: "VillageMeetingCreate",
        component: VillageMeetingCreate,
        meta: { title: "村会议新增", activeMenu: "/release/village-meeting"  },
        hidden: true,
      },
      {
        path: "village-meeting-edit/:id",
        name: "VillageMeetingEdit",
        component: VillageMeetingCreate,
        props: true,
        meta: { title: "村会议编辑", activeMenu: "/release/village-meeting"  },
        hidden: true,
      },
      {
        path: "about-village-action",
        component: VillageActionWrap,
        redirect: "noRedirect",
        name: "AboutVillageAction",
        meta: { title: "村组织活动" },
        children: [
          {
            path: "village-action-type",
            name: "VillageActionType",
            component: VillageActionType,
            meta: { title: "村组织活动类型" },
          },
          {
            path: "village-action",
            name: "VillageAction",
            component: VillageAction,
            meta: { title: "村组织活动信息" },
          },
          {
            path: "village-action-create",
            name: "VillageActionCreate",
            component: VillageActionCreate,
            meta: { title: "村组织活动新增", activeMenu: "/release/about-village-action/village-action"  },
            hidden: true,
          },
          {
            path: "village-action-edit/:id",
            name: "VillageActionEdit",
            component: VillageActionCreate,
            props: true,
            meta: { title: "村组织活动编辑", activeMenu: "/release/about-village-action/village-action"  },
            hidden: true,
          },
        ],
      },
      {
        path: "about-org-action",
        component: OrgActionWrap,
        name: "AboutOrgAction",
        meta: { title: "党组织活动" },
        children: [
          {
            path: "org-action-type",
            component: OrgActionType,
            name: "OrgActionType",
            meta: { title: "党组织活动类型" },
          },
          {
            path: "org-action",
            name: "OrgAction",
            component: OrgAction,
            meta: { title: "党组织活动信息" },
          },
          {
            path: "org-action-create",
            name: "OrgActionCreate",
            component: OrgActionCreate,
            meta: { title: "党组织活动新增", activeMenu: "/release/about-org-action/org-action"  },
            hidden: true,
          },
          {
            path: "org-action-edit/:id",
            name: "OrgActionEdit",
            component: OrgActionCreate,
            props: true,
            meta: { title: "党组织活动编辑", activeMenu: "/release/about-org-action/org-action"  },
            hidden: true,
          },
        ],
      },
    ],
  },
  {
    path: "/feedback",
    component: Layout,
    redirect: "noRedirect",
    name: "Feedback",
    meta: { title: "群众意见建议处理" },
    children: [
      {
        path: "create-feedback",
        name: "CreateFeedback",
        component: CreateFeedback,
        meta: { title: "新增反馈", "permissions": ["root", "admin", "leader"]},
      },
      {
        path: "to-be-processed",
        name: "ToBeProcessed",
        component: ToBeProcessed,
        meta: { title: "未处理意见建议" },
      },
      {
        path: "processed",
        name: "Processed",
        component: Processed,
        meta: { title: "已处理意见建议" },
      },
      {
        path: "up-processed",
        name: "UpProcessed",
        component: Upward,
        meta: { title: "向上级反馈意见建议" },
      },
    ],
  },
  {
    path: "/admin",
    component: Layout,
    redirect: "noRedirect",
    name: "Admin",
    meta: { 
      title: "管理员管理", 
      "permissions": ["root", "admin", "leader", "area", "town"],
      icon: "user-friends" 
    },
    children: [
      {
        path: "userManagement",
        name: "UserManagement",
        component: Admin,
        meta: { title: "用户管理", "permissions": ["root", "admin", "leader", "area", "town"] },
      },
      // {
      //   path: "roleManagement",
      //   name: "RoleManagement",
      //   component: () =>
      //     import("@/views/personnelManagement/roleManagement/index"),
      //   meta: { title: "角色管理", permissions: ["admin"] },
      // },
    ],
  },
  {
    path: "/logger",
    component: Layout,
    redirect: "noRedirect",
    name: "Logger",
    meta: { title: "日志", "permissions": ["root", "admin", "leader", "area"] },
    children: [
      {
        path: "user-logger",
        name: "UserLogger",
        component: Logger,
        meta: { title: "用户日志", "permissions": ["root", "admin", "leader", "area"] },
      },
      // {
      //   path: "admin-logger",
      //   name: "AdminLogger",
      //   component: Admin,
      //   meta: { title: "系统审计日志", "permissions": ["root", "admin", "leader"] },
      // },
    ],
  },
];


export const constantRoutes = [
  {
    path: "/login",
    // component: MyLogin,
    component: MyLogin,
    hidden: true,
    meta: { isPublic: true }
  },
  {
    path: "/401",
    name: "401",
    component: () => import("@/views/401"),
    hidden: true,
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index"),
      },
    ],
  },
];



const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => console.log(err));
};

console.log(constantRoutes)
const router = new VueRouter({
  mode: "hash",
  scrollBehavior: () => ({
    y: 0,
  }),
  routes: constantRoutes,
});


export function resetRouter() {
  router.matcher = new VueRouter({
    mode: "hash",
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: constantRoutes,
  }).matcher;
}

router.beforeEach((to, from ,next) => {
  console.log(to, from)
  console.log(!to.meta.isPublic, !store.state.user.accessToken)
  console.log(store.state.user.accessToken)
  if (!to.meta.isPublic && !store.state.user.accessToken) {
    return next(`/login?redirect=${from.path}`)
  }
  next()
})
export default router;
